package com.logreview;

import com.logreview.consumer.LoggerConsumer;
import com.logreview.produce.LoggerProduce;
import com.logreview.util.ContextUtil;
import com.logreview.util.FileUtil;
import com.logreview.vo.LoggerVO;
import com.logreview.vo.Monitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ApplicationInit {

    Logger logger = LoggerFactory.getLogger(ApplicationInit.class);

    @Autowired
    ApplicationProperties applicationProperties;

    @Autowired
    ArrayBlockingQueue<LoggerVO> blockingQueue;

    public ApplicationInit(){



        LoggerConsumer consumer = ContextUtil.getBean("loggerConsumer",LoggerConsumer.class);
        LoggerConsumer consumer1 = ContextUtil.getBean("loggerConsumer",LoggerConsumer.class);
        LoggerConsumer consumer2 = ContextUtil.getBean("loggerConsumer",LoggerConsumer.class);
        LoggerProduce produce = ContextUtil.getBean("loggerProduce",LoggerProduce.class);


        //MONITOR
        Runnable monitor = new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String line = "["+format1.format(new Date(System.currentTimeMillis()))+"]" + "procude:"+Monitor.procedureCount + ",consumer:" + Monitor.consumerCount;
                                line += String.format(",queue.size:%s",blockingQueue.size());
                                line += String.format(",logger.pointer:%s",FileUtil.getPointer());
                               line += "\r\n";
                    String fileName = "hds.logreview." + format.format(new Date(System.currentTimeMillis())) +".summary.log";
                    String filePath = applicationProperties.getLogPath() + fileName;
                    try {
                        logger.info(line);
                        FileUtil.append(filePath,line);
                    } catch (IOException e) {
                        logger.error("append failed",e);
                    }

                    try {
                        Thread.sleep(30*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        //设置读文件的起始位置
        ApplicationProperties ap = ContextUtil.getBean("applicationProperties",ApplicationProperties.class);
        FileUtil.setPointer(ap.getLoggerPointer());

        //execute
        ExecutorService executorService = Executors.newFixedThreadPool(15);
        executorService.execute(produce);
        executorService.execute(consumer);
        executorService.execute(consumer1);
        executorService.execute(consumer2);
        executorService.execute(monitor);

        logger.info("loggerReview init success");
    }
}
